package com.xirui.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xirui.bean.Dormitory;
import com.xirui.bean.Floor;
import com.xirui.bean.Record;
import com.xirui.bean.User;
import com.xirui.service.DorService;

@Controller
public class DorController {

	
	@Autowired
	private DorService dorService;
	
	
	@RequestMapping("tologin")
	public String tologin(){
		
		return "login";
	}
	
	@RequestMapping("login")
	public String login(User user,HttpSession session,Model model){
		User u=dorService.login(user);
		session.setAttribute("u", u);
		if(u!=null){
			return "redirect:list";
		}else{
			model.addAttribute("erry", "用户名或密码错误");
			return "login";
		}
		
	}
	
	
	@RequestMapping("list")
	public String linked(@RequestParam(defaultValue="1")Integer pageNum,Model model,HttpServletRequest request,Dormitory dor){
		User u =(User) request.getSession().getAttribute("u");
		model.addAttribute("u", u);
		if(u.getLev()==0){
			Record r=dorService.selectR(u.getUid());
			model.addAttribute("r", r);
			PageInfo<Record> list=dorService.YHlinked(pageNum,u.getUid());
			model.addAttribute("pg", list);
			return "list";
		}else{
			List<Floor> floor=dorService.selectF();
			model.addAttribute("floor", floor);
			PageInfo<Dormitory> list=dorService.GLlinked(pageNum,dor);
			model.addAttribute("pg", list);
			return "list";
		}
		
	}
	
	
	
	@RequestMapping("toAdd")
	public String toAdd(Model model){
		List<User> user=dorService.selectU();
		model.addAttribute("user", user);
		return "add";
	}
	
	@RequestMapping("add")
	public String add(Integer uid,Integer did){
		dorService.updateOutTime(uid,new Date());
		dorService.addR(uid,did);
		return "redirect:list";
	}
	
	
	@RequestMapping("selectR2")
	@ResponseBody
	public Record selectR2(Integer did){
		
		return dorService.selectR2(did);
	}
	
	
	
	@RequestMapping("liandongF")
	@ResponseBody
	public List<Floor> liandongF(){
		
		return dorService.liandongF();
	}
	
	@RequestMapping("liandongD")
	@ResponseBody
	public List<Dormitory> liandongD(Integer fid){
		
		return dorService.liandongD(fid);
	}
	
	
	
}
